package com.nnxieli.trans.base

import android.app.Activity
import android.content.Intent
import android.content.res.Configuration
import android.content.res.Resources
import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.view.View
import android.view.WindowManager
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.ViewModelProvider

/**
 *
 */
abstract class BaseActivity<T : ViewDataBinding>() : AppCompatActivity() {

    protected val mHandler: Handler = Handler(Looper.myLooper()!!)
    private var toast: Toast? = null
    lateinit var mDataBinding: T

    abstract val layoutId: Int? // xml布局文件，子类指定

    /**
     * UI初始化，[子类必须实现]
     * 会在onCreate方法中获取DataBinding和ViewModel之后被调用
     */
    protected abstract fun initView(savedInstanceState: Bundle?)

    /**
     * 绑定监听事件
     */
    open fun bindEvent() {}

    /**
     * 数据初始化，[子类可选实现]
     * 会在onCreate方法中initView方法之后被调用
     */
    open fun initData(savedInstanceState: Bundle?) {}

    /**
     * 提供ViewModelFactory
     * @return ViewModelProvider.Factory
     */
    open fun provideViewModelFactory(): ViewModelProvider.Factory {
        return ViewModelProvider.AndroidViewModelFactory(application);
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mDataBinding = providerViewDataBinding()
        mDataBinding.lifecycleOwner = this

        hideStatusBar()
        initView(savedInstanceState)
        bindEvent()
        initData(savedInstanceState)
    }

    protected fun hideStatusBar() {
        // 5.0以上系统状态栏透明
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
            //白色SYSTEM_UI_FLAG_LAYOUT_STABLE、深色SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
            window.decorView.systemUiVisibility =
                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
            window.statusBarColor = Color.TRANSPARENT
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
        }
    }

    /**
     * 获取DataBinding
     */
    private fun providerViewDataBinding(): T {
        return DataBindingUtil.setContentView(this, layoutId!!)
    }

    /**
     * 设置状态栏颜色
     * @param color String
     */
    protected fun setStatusBarColor(color: String) {
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
        window.statusBarColor = Color.parseColor(color)
    }

    /**
     * Toast
     */
    protected fun showToast(msg: String){
        toast?.cancel()
        toast = Toast.makeText(this, msg, Toast.LENGTH_SHORT)
        toast?.setText(msg) //纯净版的提示语
        toast?.show()
    }

    override fun onConfigurationChanged(newConfig: Configuration) {
        if (newConfig.fontScale != 1.0f) {
            resources
        }
        super.onConfigurationChanged(newConfig)
    }

    override fun getResources(): Resources? {
        val res: Resources? = super.getResources()
        if (res != null) {
            val config: Configuration? = res.configuration
            if (config != null && config.fontScale != 1.0f) {
                config.fontScale = 1.0f
                res.updateConfiguration(config, res.displayMetrics)
            }
        }
        return res
    }

    open fun startActivity(activity: Class<out Activity>){
        startActivity(Intent(this, activity))
    }
}